package com.example.lostandfoundsystem.web.controllers.system.user;

import com.example.lostandfoundsystem.models.system.Admin;
import com.example.lostandfoundsystem.models.system.User;
import com.example.lostandfoundsystem.services.common.ResultService;
import gaarason.database.appointment.Paginate;
import gaarason.database.contract.eloquent.Builder;
import gaarason.database.contract.eloquent.Record;
import gaarason.database.exception.EntityNotFoundException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author lucifer
 * @since 2024-08-04-10:25
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    User.Model userModel;

    // 获取单个用户信息
    @GetMapping("/item")
    ResultService.Result item(@RequestParam Map<String, String> params) {
        // 返回值
        ResultService.Result result = ResultService.getNormalResult();
        Record<User, Long> user;
        if (Objects.isNull(params.get("id"))) {
            // 用户不存在
            result.code = ResultService.NOT_FOUND;
            result.msg = "用户不存在";
            return result;
        } else {
            try {
                user = userModel.newQuery().findOrFail(params.get("id"));
            } catch (EntityNotFoundException e) {
                // 找不到就报错
                result.code = ResultService.NOT_FOUND;
                result.msg = "用户不存在";
                return result;
            }
            // 正常返回结果
            result.data = user.getEntity();
        }
        return result;
    }

    // 获取所有用户信息
    @GetMapping("/index")
    ResultService.Result index(@RequestParam Map<String, String> params) {
        // 分页单独处理
        String page = params.get("page");
        if (Objects.isNull(page)) {
            // 如果没有页数，默认为第一页
            page = "1";
        }
        // 生成查询器
        Builder<User, Long> builder = userModel.newQuery();
        // 放入参数
        params.forEach((key, value) -> {
            if (!Objects.isNull(key) && !key.equals("page")) {
                // 要去掉page这个参数，如果还要去掉其他就直接在这里加
                if (!Objects.isNull(value)) {
                    builder.where(key, value);
                }
            }
        });

        // 返回结果,每页十五条记录
        Paginate<User> paginate = builder.paginate(page, 15);

        // 对结果进行处理
        Map<String, Object> data = new HashMap<>();
        data.put("currentPage", paginate.getCurrentPage());
        data.put("lastPage", paginate.getLastPage());
        data.put("total", paginate.getTotal());
        data.put("perPage", paginate.getPerPage());
        data.put("data", paginate.getItemList());
        ResultService.Result result = ResultService.getNormalResult();
        result.data = data;
        return result;
    }

    // 修改管理员列表
    @PostMapping("/change")
    ResultService.Result change(@RequestParam Map<String, String> params) {
        // 返回值
        ResultService.Result result = ResultService.getNormalResult();
        Record<User, Long> user;
        if (Objects.isNull(params.get("id"))) {
            // 新建
            user = userModel.newRecord();
        } else {
            // 更新
            try {
                user = userModel.newQuery().findOrFail(params.get("id"));
            } catch (EntityNotFoundException e) {
                // 找不到就报错
                result.code = ResultService.NOT_FOUND;
                result.msg = "id not found";
                return result;
            }
        }
        // 填充对应参数
        user.getEntity().setNumber(params.get("number"));
        user.getEntity().setName(params.get("name"));
        user.getEntity().setGender(params.get("gender"));
        user.getEntity().setGrade(params.get("grade"));
        user.getEntity().setCollege(params.get("college"));
        user.getEntity().setMajor(params.get("major"));
        user.getEntity().setClazz(params.get("clazz"));
        user.getEntity().setPhoneType(params.get("phoneType"));
        user.getEntity().setPhoneValue(params.get("phoneValue"));
        user.save();
        return result;
    }

    // 删除用户列表
    @PostMapping("/delete")
    ResultService.Result delete(@RequestParam Map<String, String> params) {
        // 返回值
        ResultService.Result result = ResultService.getNormalResult();
        if (!Objects.isNull(params.get("id"))) {
            // 删除
            try {
                userModel.newQuery().findOrFail(params.get("id")).delete();
            } catch (EntityNotFoundException e) {
                // 找不到就报错
                result.code = ResultService.NOT_FOUND;
                result.msg = "id not found";
                return result;
            }
        }
        return result;
    }
}
